package com.union.core.sys.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.union.core.sys.model.Role;
import com.union.core.sys.model.SysModuleType;
import com.union.core.sys.service.RoleService;
import com.union.core.sys.service.SysLogsService;
import com.union.core.sys.service.SysService;
/**
 * Action类: 角色
 * filename: RoleAction.java
 * project:  union
 * @company Hainan Jowe Technology Ltd
 * @author Harry Zhang
 * @create in 2012-10-29
 * @version 1.0
 */
public class RoleAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4538771320689086966L;
	private SysService sysService = SysService.getInstance();
	//分页设置
	private int page;			//页码
	private int intRowCount;	//总行数
	private int intPageCount;	//总页数
	private int start;				//开始
	private int intPageSize=15;	//每页显示的行数
	private int intPage;		//页数变量
	//角色属性
	private int roleId;
	private String roleName;
	private String addManager;
	private String addTime;
	private int roleType;
	private int orgId;

	//查询参数
	private String parName;
	private String parVal;
	private String orderBy;


	//反馈信息
	private String operRes;

	//角色服务
	private RoleService roleSer;
	private List<Role> roleList;
	private Role role;
	//角色赋权
	private List<SysModuleType> sysModuleTypeList;
	private String moduleMgrPurview[];
	//校验
	private InputStream inputStream;
	//日志
	private SysLogsService sysLogsService;
	//权限控制
	private List<String> allPower;

	public RoleAction(){
		roleSer=new RoleService();
		sysLogsService=new SysLogsService();
	}
	public String index(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			orgId=sysService.getSessionListener().getPERSONNEL().getOrganization_id();
			lookFor();
			return SUCCESS;
		}
	}
	public String addRoleLoad(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			
			orgId=sysService.getSessionListener().getPERSONNEL().getOrganization_id();
			return SUCCESS;
		}
	}
	
	public String addRole(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}
		else{
			addManager=sysService.getSessionListener().getMANAGER_NAME();
			orgId=sysService.getSessionListener().getPERSONNEL().getOrganization_id();
			role=new Role();
			role.setRole_name(roleName);
			role.setRole_type(roleType);
			role.setOrg_id(orgId);
			role.setAdd_manager(addManager);
			boolean bl=roleSer.addRole(role);
			if(bl){
				operRes="很抱歉！添加角色【"+roleName+"】失败";
			}else{
				sysLogsService.addSysLogs("添加角色【"+roleName+"】");
				operRes="添加角色【"+roleName+"】成功";
			}
			return SUCCESS;
		}
	}
	public String empowerLoad(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}
		else{
			//判断授权人所拥有的权限
			//用户权限
			String useracl=sysService.getSessionListener().getMODULE_MGR_PURVIEW();
			String roleList=sysService.getSessionListener().getMANAGER_ROLES();
			//角色权限
				//整合角色权限
				Set<String> set = new HashSet<String>();
				
				if(roleList!=null && !"".equals(roleList)){
					for(String rid : roleList.split("\\|")){
						if(!"".equals(rid)){
						role=roleSer.queryById(Integer.parseInt(rid));
						String rpu=role.getRole_permissions();
						if(rpu!=null && !"".equals(rpu)){
							for(String pu : rpu.split("\\|")){
								if(!"".equals(pu)){
									set.add(pu);
								}
							}
						}
					}
					}
				}
				String abc="|";
				//整合用户与角色权限
				if(useracl!=null && !"".equals(useracl)){
					for(String pu : useracl.split("\\|")){
							if(!"".equals(pu)){
								set.add(pu);
							}
						}
				}
				//返回权限
		        for(Object s: set.toArray()){
		        	abc=abc+s.toString()+"|";
		        }
		        sysModuleTypeList=roleSer.editload(roleId, abc);
		        role=roleSer.queryById(roleId);
			return SUCCESS;
		}
	}

	public String empowerTorole(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}
		else{
			role=roleSer.queryById(roleId);
			boolean bl=roleSer.empower(roleId,moduleMgrPurview);
			
			if(bl){
				operRes="恭喜！为角色【"+role.getRole_name()+"】授权成功";
				sysLogsService.addSysLogs("为角色【"+role.getRole_name()+"】授权");
			}else{
				operRes="很遗憾！为角色【"+role.getRole_name()+"】授权失败";
			}
			return SUCCESS;
		}
	}
	public List<SysModuleType> getSysModuleTypeList() {
		return sysModuleTypeList;
	}
	public void setSysModuleTypeList(List<SysModuleType> sysModuleTypeList) {
		this.sysModuleTypeList = sysModuleTypeList;
	}
	public String delRole(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}
		else{
			role=roleSer.queryById(roleId);
			boolean bl=roleSer.delRole(roleId);
			if(bl){
				operRes="很抱歉！删除角色【"+role.getRole_name()+"】失败";
			}else{
				sysLogsService.addSysLogs("删除角色：【"+role.getRole_name()+"】");
				operRes="删除角色【"+role.getRole_name()+"】成功";
			}
			return SUCCESS;
		}
	}
	private void lookFor(){
		if(page<1){
			intPage = 1;
			page=1;
		}else{
			intPage = page;
		}
		intRowCount  = roleSer.queryRoleListCount(roleId, roleName, addManager, addTime, parName,roleType,orgId, parVal);
		intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
		if(intPage > intPageCount) intPage = intPageCount;		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
		roleList=roleSer.queryRoleList(roleId, roleName, addManager, addTime, parName,roleType,orgId, parVal, orderBy, start, intPageSize);
		}
	}
	public String checkRoleName(){
		Boolean canSign = false;
		boolean result = roleSer.checkRoleName(roleName);
		if(result==true){//存在,要返回false
			canSign = false;
		}else{
			canSign = true;
		}
		inputStream = new ByteArrayInputStream(canSign.toString().getBytes());
        return SUCCESS;
	}
	public int getIntRowCount() {
		return intRowCount;
	}
	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRoleId() {
		return roleId;
	}
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getAddManager() {
		return addManager;
	}
	public void setAddManager(String addManager) {
		this.addManager = addManager;
	}
	public String getAddTime() {
		return addTime;
	}
	public void setAddTime(String addTime) {
		this.addTime = addTime;
	}
	public String getParName() {
		return parName;
	}
	public void setParName(String parName) {
		this.parName = parName;
	}
	public String getParVal() {
		return parVal;
	}
	public void setParVal(String parVal) {
		this.parVal = parVal;
	}

	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public int getIntPageCount() {
		return intPageCount;
	}
	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}
	public String getOperRes() {
		return operRes;
	}
	public void setOperRes(String operRes) {
		this.operRes = operRes;
	}
	public String[] getModuleMgrPurview() {
		return moduleMgrPurview;
	}
	public void setModuleMgrPurview(String[] moduleMgrPurview) {
		this.moduleMgrPurview = moduleMgrPurview;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public int getRoleType() {
		return roleType;
	}
	public void setRoleType(int roleType) {
		this.roleType = roleType;
	}
	public int getOrgId() {
		return orgId;
	}
	public void setOrgId(int orgId) {
		this.orgId = orgId;
	}
	public List<String> getAllPower() {
		return allPower;
	}
	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}
}
